<template>
  <div class="home">
    <div class="container">
      <el-form :inline="true" :model="form" :rules="rules" ref="form" class="form" label-width="120px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="男" value="男"></el-radio>
          <el-radio v-model="form.sex" label="女" value="女"></el-radio>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="update">修改</el-button>

    </div>
  </div>
</template>

<script>

export default {
  name: 'EditUser',
  data() {
    const checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'));
      }
      if (!/^[0-9]+$/.test(value)) {
        callback(new Error('请输入数字值'));
      }
      if (parseInt(value) > 120 || parseInt(value) < 0) {
        callback(new Error('请输入合适的年龄'));
      }
      callback();
    };

    const checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      }
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)) {
        callback(new Error('请输入合法的手机号'));
      }
      callback();
    };
    return {
      form: {
        sex: '男'
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        age: [
          { validator: checkAge, trigger: 'blur' }
        ], phone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      const id = this.$route.query.id;
      this.request.get("/api/user/" + id).then(res => {
        this.form = res.data;
      })
    },
    update() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.request.put("/api/user/update", this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success("更新成功");
            } else {
              this.$notify.error("更新失败");
            }

            this.form = {};
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    }
  }
}
</script>


<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;

  .container {
    width: 800px;
    height: 500px;

    margin-top: 80px;
    background-color: #ffffff;
    border-radius: 25px;
    box-shadow: 0 3px 5px #ccc;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;

    .form {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
  }
}
</style>